class PWNHUD extends UDKHUD;


/* The Pawn that is currently owning this hud */
var Pawn PawnOwner;

/* Points to the UT Pawn.  Will be resolved if in a vehicle */
var PWNPawn PWNPawnOwner;

/* Cached reference to the another hud texture */
var const Texture2D HudTexture;

var LinearColor HudTint;

var bool bShowAmmo;
var Vector2D AmmoPosition;
var TextureCoordinates AmmoBGCoords;

/* Resolution dependent HUD scaling factor */
var float HUDScaleX, HUDScaleY;
/* Holds the scaling factor given the current resolution.  This is calculated in PostRender() */
var float ResolutionScale, ResolutionScaleX;

/* The percentage of the view that should be considered safe */
var float SafeRegionPct;
/* Holds the full width and height of the viewport */
var float FullWidth, FullHeight;

/**
 * Perform any value precaching, and set up various safe regions
 *
 * NOTE: NO DRAWING should ever occur in PostRender.  Put all drawing code in DrawHud().
 */

event PostRender()
{
	PawnOwner = Pawn(PlayerOwner.ViewTarget);
	if(PawnOwner == none)
	{
		PawnOwner = PlayerOwner.Pawn;
	}

	PWNPawnOwner = PWNPawn(PawnOwner);

	//Draw any debug text in realtime
	PlayerOwner.DrawDebugTextList(Canvas, RenderDelta);

	HUDScaleX = Canvas.ClipX/1280;
	HUDScaleY = Canvas.ClipY/1280;

	ResolutionScaleX = Canvas.ClipX/1024;
	ResolutionScale = Canvas.ClipY/768;

	FullWidth = Canvas.ClipX;
	FullHeight = Canvas.ClipY;

	if (bShowHUD)
	{
		DrawHUD();
	}

	//Draw always present overlays
	DrawInputZoneOverlays();
}

/**
 * This is the main drawing pump.  It will determine which hud we need to draw (Game or PostGame).  Any drawing that should occur
 * regardless of the game state should go here.
 */

function DrawHUD()
{
	local float x, y, w, h;

	//Create safe region
	w = FullWidth*SafeRegionPct;
	x = Canvas.OrgX + (Canvas.ClipX - w) * 0.5;

	// We have some extra logic for figuring out how things should be displayed
	// in split screen.
	h = FullHeight * SafeRegionPct;
	y = Canvas.OrgY + (Canvas.ClipY - h) * 0.5;

	Canvas.OrgX = x;
	Canvas.OrgY = y;
	Canvas.ClipX = w;
	Canvas.ClipY = h;
	Canvas.Reset(true);

	//Set up DeltaTime
	RenderDelta = WorldInfo.TimeSeconds - LastHUDRenderTime;
	LastHUDRenderTime = WorldInfo.TimeSeconds;

	PlayerOwner.DrawHUD(Self);

	if (bShowGameHud)
	{
		DrawGameHud();
	}
}

function DrawGameHUD()
{
	DisplayLocalMessages();
	DisplayConsoleMessages();    
	DrawLivingHud();
}

/**
 * Anything drawn in this function will be displayed ONLY when the player is living.
 */

function DrawLivingHUD()
{
	local UTWeapon Weapon;

	// Manage the weapon.  NOTE: Vehicle weapons are managed by the vehicle
	// since they are integrated in to the vehicle health bar

	if(PawnOwner != none)
	{
		if(bShowAmmo)
		{
			Weapon = UTWeapon(PawnOwner.Weapon);
			if(Weapon != none)
			{
				DisplayAmmo(Weapon);
			}
		}
	}
}

function DisplayAmmo(UTweapon Weapon)
{
	local Vector2D POS;
	local string Amount;
	local int AmmoCount;

	//Resolve the position
	POS = ResolveHUDPosition(AmmoPosition, AmmoBGCoords.UL,AmmoBGCoords.VL);

	//Figure out if we should be pulsing
	AmmoCount = Weapon.GetAmmoCount();
	
	//Draw the background
	Canvas.SetPos(POS.X,POS.Y);// - (AmmoBarOffsetY * ResolutionScale));
	Canvas.DrawTile(HudTexture, AmmoBGCoords.UL * ResolutionScale, AmmoBGCoords.VL * ResolutionScale,AmmoBGCoords.U, AmmoBGCoords.V,AmmoBGCoords.UL,AmmoBGCoords.VL, HudTint);
	

	//Draw the amount
	Amount = ""$AmmoCount;
	Canvas.DrawColor = WhiteColor;
	Canvas.DrawText(Amount,,3.0f,3.0f);

}


/**
 * Given a default screen position (at 1024x768) this will return the hud position at the current resolution.
 * NOTE: If the default position value is < 0.0f then it will attempt to place the right/bottom face of
 * the "widget" at that offset from the ClipX/Y.
 *
 * @Param Position&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;The default position (in 1024x768 space)
 * @Param Width&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;How wide is this "widget" at 1024x768
 * @Param Height&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;How tall is this "widget" at 1024x768
 *
 * @returns the hud position
 */
function Vector2D ResolveHUDPosition(vector2D Position, float Width, float Height)
{
 local vector2D FinalPos;
 FinalPos.X = (Position.X < 0) ? Canvas.ClipX - (Position.X * ResolutionScale) - (Width * ResolutionScale) : Position.X * ResolutionScale;
 FinalPos.Y = (Position.Y < 0) ? Canvas.ClipY - (Position.Y * ResolutionScale) - (Height * ResolutionScale) : Position.Y * ResolutionScale;

 return FinalPos;
}

defaultproperties
{
 HudTexture=Texture2D'PWNPackage.PWNTextures.wall_512_1_05'

 HudTint=(R=1.0f,G=1.0f,B=1.0f,A=1.0f)

 AmmoPosition=(X=0,Y=-1)
 AmmoBGCoords=(U=0,UL=76,V=0,VL=126)

 SafeRegionPct = 1.0f

 bShowAmmo=true
}

 